﻿@model PageModel
@inject SeoSettings seoSettings
@inject IPageHeadBuilder pagebuilder
@{
    var isPopup = ViewBag.IsPopup;
    if (isPopup == null || isPopup == false)
    {
        Layout = "_SingleColumn";
    }
    else
    {
        /*pop-up windows*/
        Layout = "_LayoutPopup";
    }

    if (!Model.IsPasswordProtected)
    {
        pagebuilder.AddTitleParts(!string.IsNullOrEmpty(Model.MetaTitle) ? Model.MetaTitle : Model.Title);
        pagebuilder.AddMetaDescriptionParts(Model.MetaDescription);
        pagebuilder.AddMetaKeywordParts(Model.MetaKeywords);
    }

    var canonicalUrlsEnabled = seoSettings.CanonicalUrlsEnabled;
    if (canonicalUrlsEnabled)
    {
        var pageUrl = Url.RouteUrl("Page", new { Model.SeName }, Url.ActionContext.HttpContext.Request.Scheme);
        pagebuilder.AddCanonicalUrlParts(pageUrl);
    }
}
@if (Model.IsPasswordProtected)
{
    // For popup pages, use a minimal layout that includes the Axios and scripts
    if (isPopup != null && isPopup == true)
    {
        Layout = "_LayoutPopup";
    }

    <script asp-location="Footer" asp-order="300">
        function passwordProtect() {
            var postData = {
                id: document.getElementById("@Html.IdFor(model => model.Id)").value,
                password: document.getElementById('password').value
            };
            addAntiForgeryToken(postData);

            var bodyFormData = new FormData();
            bodyFormData.append('id', document.getElementById("@Html.IdFor(model => model.Id)").value);
            bodyFormData.append('password', document.getElementById('password').value);
            bodyFormData.append('__RequestVerificationToken', document.querySelector('input[name=__RequestVerificationToken]').value);
            axios({
                method: 'post',
                url: '@Url.RouteUrl("PageAuthenticate")',
                data: bodyFormData,
                headers: { 'Content-Type': 'multipart/form-data' }
            }).then(function (response) {
                if (response.data.Authenticated) {
                    document.querySelector('#ph-page #ph-title h1').innerHTML = response.data.Title;
                    if (document.querySelector('#ph-page #ph-title h1').innerText == '') {
                        document.getElementById('ph-title').style.display = "none";
                    }
                    document.querySelector('#ph-page .page-body').innerHTML = response.data.Body;
                    document.getElementById('ph-password').style.display = "none";
                    document.getElementById('ph-page').style.display = "block";
                }
                else {
                    document.getElementById('password-error').innerText = response.data.Error;
                }
            })
        }

        document.getElementById('ph-page').style.display = "none";        
        document.addEventListener("keydown", (e) => {
            if (e.key == "Enter"){
                passwordProtect();
            }
        });
    
</script>
    <div class="page page-password" id="ph-password">
        <form id="passwordPage" asp-route="PageAuthenticate" method="post" onsubmit="return false">
            <input asp-for="Id" type="hidden"/>
            <div class="enter-password-title">
                @Loc["Page.EnterPassword"]
            </div>
            <div class="enter-password-form">
                @Html.Password("password", null, new { @class = "form-control mt-1" })
                <b-button id="button-password" onclick="passwordProtect()" variant="secondary" class="page-password-button mt-1 mb-1">
                    @Loc["Page.Button"]
                </b-button>
            </div>
            <div class="password-error text-danger h5 mt-1 mb-1">
                <span id="password-error"></span>
            </div>
        </form>
    </div>
}
<div class="page page-page" id="ph-page">
    <div class="page-title" id="ph-title">
        <h1>@Model.Title</h1>
    </div>
    <div class="page-body">
        @Html.Raw(Model.Body)
    </div>
</div>